extends "../yxinfo.gd"

var tpView #护盾条相关
var shield:int = 0 #护盾值
var shieldLimit #护盾上限
var killNum = 0
var battletime:int = 0
var canre = true
var camp = null
var ischange = false#被转换为死灵
var yuanteam

#初始化
func _info():
	pass
func _connect():
	._connect()
	pass
func _init():
	._init()
	if not is_connected("onHurtEnd",self,"HE"):
		connect("onHurtEnd", self, "HE")
	
func _extInit():
	._extInit()
	if not is_connected("onPressed", self, "chaInfo"):
		connect("onPressed", self, "chaInfo")
	#if not is_connected("onAddItem", self, "lvups"):
	#	connect("onAddItem", self, "lvups")
	
	tpViewInit()
#————————————————————————————————————————————————护盾相关————————————————————————————————————————————————#
#护盾条初始化
func tpViewInit():
	if isSumm:return
	tpView = TextureProgress.new()
	var imt = utils.cIt("cex___toolman/mp.png")
	tpView.set_progress_texture(imt)
	tpView.set_fill_mode(4)
	tpView.set_max(att.maxHp)
	tpView.set_step(0.01)
	tpView.set_min(0)
	tpView.set_value(0)
	tpView.set_position(Vector2(-63,-25))
	add_child(tpView)
#改变护盾值
func changeShield(num):
	if num > 0:
		num *= att.reHp + 1
	num *= globalBonus.shieldBonus
	shield += num
	if shieldLimit == null and shield > att.maxHp:
		shield = att.maxHp
	elif shieldLimit != null and shield > shieldLimit:
		shield = shieldLimit
	shield = int(shield)
	tpView.set_value(shield)
#————————————————————————————————————————————————护盾相关————————————————————————————————————————————————#
#杀敌数统计
func _onKillChara(atkInfo:AtkInfo):
	._onKillChara(atkInfo)
	killNum += 1

func _onAtkChara(atkInfo):
	pass
#————————————————————————————————————————————————————统计数据————————————————————————————————————————————————————#
func _onHurt(atkInfo):#待修订护盾伤害加成
	._onHurt(atkInfo)
	#if atkInfo.isMiss:return
	if atkInfo.atkCha == null:
		atkInfo.hurtVal = 0
	#护盾判定
	if shield == 0:return
	var bonus = 1
	#if atkInfo.hurtType == Chara.HurtType.MGI:
	#	bonus = 1.3
	shield -= atkInfo.hurtVal * bonus
	emit_signal("afterhurtshield")
	if shield >= 0:
		changeStatis(atkInfo.atkCha,atkInfo.hitCha,atkInfo.hurtVal * bonus)
		atkInfo.hurtVal = 0
	else:
		atkInfo.hurtVal -= abs(shield)
		changeStatis(atkInfo.atkCha,atkInfo.hitCha,abs(shield))
		shield = 0
	tpView.set_value(shield)
#敌方造成伤害和承受伤害
func HE(atkInfo):
	if atkInfo.hurtVal > 0 && not atkInfo.isMiss and atkInfo.atkCha.team != atkInfo.hitCha.team:
		changeStatis(atkInfo.atkCha, atkInfo.hitCha, atkInfo.hurtVal)
#修改统计
func changeStatis(a,b,num):#攻击者，承受者，伤害
	var hitStatis = ctrl.getStatisByCha(b)
	var atkStatis = ctrl.getStatisByCha(a) 
	hitStatis.hurt += num
	atkStatis.damage += num
	var s1 = hitStatis.get(a,{"dmg":0,"hurt":0,"heal":0,"name":a.chaName})
	s1.hurt += num
	hitStatis[a] = s1
	var s2 = atkStatis.get(b,{"dmg":0,"hurt":0,"heal":0,"name":b.chaName})
	s2.dmg += num
	atkStatis[b] = s2
	ctrl.setStatisByCha(a, atkStatis)
	ctrl.setStatisByCha(b, hitStatis)
#治疗并统计
func healCha(cha, num):
	emit_signal("onHealCha",cha,num)
	if cha.team == 2 or num > 0:
		num = min(cha.att.maxHp - cha.att.hp, num)
		cha.plusHp(num)
	if num > 0:
		increDealNum(cha,num)
#写入统计
func increDealNum(cha,num):
	var statis = ctrl.getStatisByCha(self)
	statis.deal += num
	var s1 = statis.get(cha,{"dmg":0,"hurt":0,"heal":0,"name":cha.chaName})
	s1.heal += num
	statis[cha] = s1
	ctrl.setStatisByCha(self, statis)
#————————————————————————————————————————————————————统计数据————————————————————————————————————————————————————#
signal onBattleEnd
signal onBattleStart
signal afterhurtshield
#signal onAddItem
#signal onDelItem
signal onHealCha
signal upS
func _onBattleEnd():
	._onBattleEnd()
	battletime = 0
	emit_signal("onBattleEnd")
	var chas = getAllChas(2)
	for i in chas:
		if i.ischange:
			i.ischange = false
			i.attInfo.atk = i.attInfo.atk/0.25
			i.upAtt()
			i.healCha(i,i.att.maxHp)
			match i.lv:
				1:
					sys.main.player.plusHp(-2)
				2:
					sys.main.player.plusHp(-5)
				3:
					sys.main.player.plusHp(-10)
				4:
					sys.main.player.plusHp(-15)
	changeShield(-shield)
	#healp = tmphealp
func _onBattleStart():
	._onBattleStart()
	self.llv = min(5,self.llv)
	if self.llv:
		addBuff(b_lvup.new(self.llv))
	yuanteam = self.team
	battletime = 0
	emit_signal("onBattleStart")
	
	#tmphealp = healp
	#healp = healp + refeshhlp()
	#print(tmphealp,"    ",healp,"    ",refeshhlp())
func _upS():
	._upS()
	#tpView.set_value(shield)
	#血量为负时，执行补偿机制
	if att.hp <= 0:
		changeShield(-shield)
		hurtChara(self, 0, Chara.HurtType.REAL, Chara.AtkType.EFF)
	#超时加伤机制
	battletime += 1
	if battletime > 100:
#		var buff = hasBuff("b_atkR_diff")
#		if buff == null:
		self.addBuff(b_atkR_diff.new(0.1))
#		else:
#			buff.att.atkR += 0.1
	emit_signal("upS")

#跳跃
func jump(target, selfCha):
	var mv = Vector2(target.cell.x ,target.cell.y)
	var vs = [Vector2(0,0),Vector2(1,0),Vector2(-1,0),Vector2(0,1),Vector2(0,-1),Vector2(1,1),Vector2(-1,1),Vector2(-1,-1),Vector2(1,-1)]
	for i in vs:
		var v = mv+i
		if sys.main.matCha(v) == null && sys.main.isMatin(v):
			if selfCha.setCell(v) :
				var pos = sys.main.map.map_to_world(selfCha.cell)
				ying(pos, selfCha.position)
				selfCha.position = pos
				selfCha.aiCha = null
				break
#移动特效
func ying(pos,position):
	var l:Vector2 = pos - position
	var s = 25
	var rs = preload("res://core/ying.tscn")
	var n = l.length()/s
	for i in range(n):
		var spr = rs.instance()
		sys.main.map.add_child(spr)
		spr.texture = img.texture
		spr.position = position + s * (i+1) * l.normalized() - Vector2(img.texture.get_width()/2,img.texture.get_height())
		spr.init(255/n * i + 100)

#技能描述特效
func cst(text, color, position):
	var eff = sys.newEff("numHit", position, false, 1)
	eff.setText(text, color)
	eff.anim.set_speed_scale(0.2)
#————————————————————————————————————————————————————修改治疗效果————————————————————————————————————————————————————#
func refeshhlp():
	var tmp:int = 0
	for i in items:
		if i.get("healp") != null && i.get("healp") != 0:
			tmp += i.healp
	return tmp / 10000.0			
#————————————————————————————————————————————————————修改治疗效果————————————————————————————————————————————————————#
class b_atkR_diff:
	extends Buff
	var canremove:bool = false
	func _init(lv):
		attInit()
		id = "b_atkR_diff"
		att.atkR = lv

class b_lvup:
	extends Buff
	var canremove:bool = false
	func _init(lv):
		attInit()
		id = "b_lvup"
		att.def = 10 * lv
		att.mgiDef = 10 * lv
		att.maxHp = 150 * lv
		att.atk = 17 * lv
#—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————#
func setAiCha(cha):
	yield(sys.get_tree().create_timer(0.01),"timeout")
	if self.hasBuff("b_qx") != null:
		aiCha = cha
		return
	if cha.hasBuff("b_vague") != null && cha.team != team:
		var chas = getAllChas(1)
		chas.sort_custom(self, "sortByDistance")
		chas.erase(cha)
		for i in chas:
			if i.hasBuff("b_vague") != null:continue
			cha = i
			break
	aiCha = cha
	
func dis(a,b):
	var x = abs(a.x - b.x)
	var y = abs(a.y - b.y)
	return sqrt(x*x + y*y)